package com.cskaoyan.config.shiro;

import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * 登陆成功 获得sessionID ->返回给前端 前端发送请求的时候会把sessionID一起发送过来
 * 通过sessionManager来获得sessionID 登录成功的token就是sessionID(前端已维护)
 * 重写获得sessionId的方法
 *
 */
@Component
public class CustomSessionManager extends DefaultWebSessionManager {
    private static final String HEADER = "X-CskaoyanMarket-Admin-Token";
    private static final String WX_HEADER = "X-CskaoyanMarket-Token";
    @Override
    protected Serializable getSessionId(ServletRequest srequest, ServletResponse response) {
        HttpServletRequest request = (HttpServletRequest) srequest;
        String sessionId = request.getHeader(HEADER);
        if (sessionId != null && !"".equals(sessionId)) {
            return sessionId;
        }
        String sessionId2 = request.getHeader(WX_HEADER);
        if (sessionId2 != null && !"".equals(sessionId2)) {
            return sessionId2;
        }
        return super.getSessionId(srequest, response);
    }
}
